import java.awt.AWTException;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
 * 连连看
 * @author DQ
 * 软件2班 邓强
 */
public class LLS extends JFrame{
	
	private int rows = 8; //行
	private int cols = 8; //列
	private int level = 5;
	private int[][] checkerBoard;
	private Random rd = new Random();
	private int size = 80; //每图片的大小
	int x1, y1, x2, y2;
	int count = 1;
	
	private Image[]  images = {
			Toolkit.getDefaultToolkit().getImage(LLS.class.getResource("images/123.png")),
			Toolkit.getDefaultToolkit().getImage(LLS.class.getResource("images/3.png")),
			Toolkit.getDefaultToolkit().getImage(LLS.class.getResource("images/4.png")),
			Toolkit.getDefaultToolkit().getImage(LLS.class.getResource("images/5.png")),
			Toolkit.getDefaultToolkit().getImage(LLS.class.getResource("images/6.png")),
			Toolkit.getDefaultToolkit().getImage(LLS.class.getResource("images/7.png")),
			Toolkit.getDefaultToolkit().getImage(LLS.class.getResource("images/8.png")),
			Toolkit.getDefaultToolkit().getImage(LLS.class.getResource("images/9.png")),
			Toolkit.getDefaultToolkit().getImage(LLS.class.getResource("images/10.png"))
	};
	
	public LLS() {
		
		this.setTitle("水果连连看");
		this.setSize(cols * size + 10, rows * size + 30);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setResizable(false);																								
		this.setLayout(null);
		this.setVisible(true);		
		this.setIconImage(Toolkit.getDefaultToolkit().getImage(LLS.class.getResource("images/dq.jpg")));

		LLSPanel te = new LLSPanel();
		this.add(te);
		
	}
	

class LLSPanel extends JPanel{
		
	public LLSPanel() {
		init(); //初始化棋盘
		this.setSize(cols * size + 20, rows * size);
		this.setLayout(null);
		startClear();
	}
	
//	public void open() {
//	init(); //初始化棋盘
//	createContent(); //创建游戏界面
//	startClear();
//	}

	private void startClear() {
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if ( e.getButton() == e.BUTTON1 ){
					if( count % 2 != 0 ) {
					y1 = ( e.getX() - 4 ) / size;
					x1 = ( e.getY() - 25 ) / size;
					
					Graphics g=getGraphics();	
					g.setColor(Color.RED);
					Graphics2D g2 = (Graphics2D)g;
					g2.setStroke(new BasicStroke(4.0f));
					g2.drawRect(y1 * size + 4, x1 * size + 25, size,size);
					//System.out.println("x1 =" + x1 + ",y1 =" + y1);						
					}

					if( count % 2 == 0 ) {
						y2 = ( e.getX() - 4 ) / size;
						x2 = ( e.getY() - 25 ) / size;
						
						Graphics g=getGraphics();	
						g.setColor(Color.RED);
						Graphics2D g2 = (Graphics2D)g;
						g2.setStroke(new BasicStroke(4.0f));
						g.drawRect(y2 * size + 4, x2 * size + 25, size,size);				
						//System.out.println("x2 =" + x2 + ",y2 =" + y2);
					}
					count++;
				}
			Clear(x1, y1, x2, y2);	
			}
		});
	}

//	private void createContent() {
//
//		this.setTitle("水果连连看");
//		this.setSize(cols * size + 10, rows * size + 30);
//		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		this.setLocationRelativeTo(null);
//		this.setResizable(false);
//		
//		this.setIconImage(Toolkit.getDefaultToolkit().getImage(LLS.class.getResource("images/dq.jpg")));	
//		
//	}
	
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		
		int j = 0;
		for(int i = 0; i < rows; ++i) {
			for(j = 0; j < cols; ++j) {
				g.drawImage(images[checkerBoard[i][j]], j * size + 4, i * size + 25, size, size, null);
			}
		}
		
		// 画线
		g.setColor(Color.BLACK);
		for(int i = 0; i <= rows; ++i ) {
			g.drawLine(0, i * size + 25, cols * size + 6, i * size + 25);
		}
		
		for(int i = 0; i <= cols; ++i) {
			g.drawLine(i * size + 4, 0, i * size + 4, rows * size + 25);
		}
	}
	
	/**
	 * 初始化棋盘的方法
	 */
	private void init() {
		checkerBoard = new int[rows][cols];	
		int j = 0, temp = 0;

		if (rows % 2 == 0) { //行数是偶数
			for (int i = 0; i < rows; i += 2) {
				for (j = 0; j< cols; ++ j) {
					temp = rd.nextInt(level) + 1;
					checkerBoard[i][j] = temp;
					checkerBoard[i + 1][j] = temp;
				}
			}
		} else {
			for (int i = 0; i < rows; ++ i) {
				for (j = 0; j< cols; ++ j) {
					temp = rd.nextInt(level) + 1;
					checkerBoard[i][j] = temp;
					checkerBoard[i][ ++j ] = temp;
				}
			}
		}
		shuffle();
	}

	
	/*
	 * 打乱棋盘的方法
	 */
	private void shuffle() {
		int x1, x2, y1, y2;

		for(int i = 0; i < rows * cols * level; ++ i) {
			x1 = rd.nextInt(rows);
			y1 = rd.nextInt(cols);
			
			x2 = rd.nextInt(rows);
			y2 = rd.nextInt(cols);
			
			if(x1 == x2 && y1 == y2) {
				continue;
			}
			
			checkerBoard[x1][y1] = checkerBoard[x1][y1] + checkerBoard[x2][y2];
			checkerBoard[x2][y2] = checkerBoard[x1][y1] - checkerBoard[x2][y2];
			checkerBoard[x1][y1] = checkerBoard[x1][y1] - checkerBoard[x2][y2];
		}
	}
	
	/*
	 * 消除的方法
	 */
	private void Clear(int x1, int y1, int x2, int y2) {
		new Timer(350,new ActionListener() {    		
			@Override
			public void actionPerformed(ActionEvent e) {
			if(basiCheck(x1, y1, x2, y2)) {
				if(boundClear(x1, y1, x2, y2) || onLineClear(x1, y1, x2, y2) || 
			twoLineClear(x1, y1, x2, y2) || threeLineClear(x1, y1, x2, y2) ) {
					checkerBoard[x1][y1] = 0;
					checkerBoard[x2][y2] = 0;
					repaint();	
					if(gameOver()) {
						System.out.println("Game Over!!!");
						return;
					}
				}
			}
			}
		}).start();
	}
	
	/*
	 * 点击事件
	 */
	private boolean basiCheck(int x1, int y1, int x2, int y2) {
		if(x1 == x2 && y1 == y2) {
			return false;
		}
		if(checkerBoard[x1][y1] != checkerBoard[x2][y2]) {
			return false;
		}
		return true;
	}
	

	/*
	 * 边缘消除
	 */
	private boolean boundClear(int x1, int y1, int x2, int y2) {
		if ( (x1 == 0 && x2 == 0) || (x1 == rows - 1 && x2 == rows - 1) ) {
			return true;
		}
		if ( (y1 == 0 && y2 == 0) || (y1 == cols - 1 && y2 == cols - 1) ) {
			return true;
		}
		return false;
	}
	
	/*
	 * 直线消除
	 */
	private boolean onLineClear(int x1, int y1, int x2, int y2) {
		if(x1 == x2) {
			int max = y1 > y2 ? y1 : y2;
			int min = y1 > y2 ? y2 : y1;
			
			for(int i = min + 1;i < max; i++) {
				if(checkerBoard[x1][i] != 0) {
					return false;
				}
			}
			
//			Graphics g=getGraphics();	
//			g.setColor(Color.RED);
//			Graphics2D g2 = (Graphics2D)g;
//			g2.setStroke(new BasicStroke(3.0f));
//			g2.drawLine(x1 + size / 2, y1 + size / 2, x2 + size / 2, y2 + size / 2);
			
			return true;
	}
		if(y1 == y2) {
			int max = x1 > x2 ? x1 : x2;
			int min = x1 > x2 ? x2 : x1;
			for(int i = min + 1;i < max; i++) {
				if(checkerBoard[i][y1] != 0) {
					return false;
				}
			}
			
//			Graphics g=getGraphics();	
//			g.setColor(Color.RED);
//			Graphics2D g2 = (Graphics2D)g;
//			g2.setStroke(new BasicStroke(3.0f));
//			g2.drawLine(x1 + size / 2, y1 + size / 2, x2 + size / 2, y2 + size / 2);
			
			return true;
	}
		return false;
	}
	/*
	 * 一折消除
	 */
	private boolean twoLineClear(int x1, int y1, int x2, int y2) {
		if(checkerBoard[x1][y2] == 0 && onLineClear(x1, y1, x1, y2) && onLineClear(x2, y2, x1, y2)) {
			return true;
		}
		if(checkerBoard[x2][y1] == 0 && onLineClear(x1, y1, x2, y1) && onLineClear(x2, y2, x2, y1)) {
			return true;
		}
		return false;
	}
	
	/*
	 * 二折消除
	 */
	private boolean threeLineClear(int x1, int y1, int x2, int y2) {
		for(int i = 0; i < rows; ++i) {
			if(i != x1 && checkerBoard[i][y1] == 0 && onLineClear(i, y1, x1, y1) && twoLineClear(i, y1, x2, y2)) {
				return true;
			}
		}
		
		for(int i = 0; i < cols; ++i) {
			if(i != y1 && checkerBoard[x1][i] == 0 && onLineClear(x1, i, x1, y1) && twoLineClear(x1, i, x2, y2)) {
				return true;
			}
		}
		return false;
	}
	
	private boolean gameOver() {
		for(int[] arr : checkerBoard) {
			for(int num : arr) {
				if(num != 0) {
					return false;
				}
			}
		}
		return true;
	}
	
	
	
	}
	

	
}